var t = getApp();

Page({
    data: {
        article_id: "",
        articleData: {},
        is_liked: 0,
        like_num: "",
        read_num: "",
        article_content: "",
        codeUrl: "",
        replyName: "",
        commentPage: 1,
        commentList: [],
        last_page: 1,
        commenMember: [],
        keyInd: 0,
        comment_id: 0,
        comment: !1,
        commentNum: 0,
        imgList: [],
        maincomment: "",
        replyContent: ""
    },
    onLoad: function(e) {
        e.mid && t._setMid(e.mid), this.setData({
            article_id: e.article_id,
            options: e
        }), this._getData();
    },
    _getData: function(e) {
        var a = this, i = t.getNetAddresss("plugin.article.api.article.get-article");
        i += "&article_id=" + a.data.article_id, t._getNetWork({
            url: i,
            success: function(t) {
                if (1 == t.data.result) {
                    var i = t.data.data;
                    if (i.content = i.content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block" '), 
                    i.has_one_article_pay && !i.has_one_record) return void wx.showToast({
                        title: "您尚未购买该文章内容,无法观看",
                        icon: "none",
                        duration: 1e3,
                        success: function(t) {
                            setTimeout(function() {
                                wx.navigateTo({
                                    url: "/packageD/article/articleList/articleList?article_id=" + a.data.article_id
                                });
                            }, 500);
                        }
                    });
                    a.setData({
                        articleData: i,
                        article_content: i.content,
                        is_liked: i.liked,
                        like_num: i.like_num + i.virtual_like_num,
                        read_num: i.read_num + i.virtual_read_num,
                        miQrCodeUrl: i.miQrCodeUrl,
                        qr: i.qr
                    }), i.comment && (a.setData({
                        comment: i.comment
                    }), a.getComment()), wx.setNavigationBarTitle({
                        title: i.title
                    }), null != e && e(t);
                } else "您尚未购买该文章内容,无法观看" == t.data.msg ? wx.showModal({
                    content: t.data.msg,
                    showCancel: !1,
                    success: function() {
                        wx.redirectTo({
                            url: "/packageD/article/articleList/articleList?article_id=" + a.data.article_id
                        });
                    }
                }) : wx.showModal({
                    content: t.data.msg,
                    showCancel: !1,
                    success: function() {
                        wx.navigateBack({
                            delta: 1
                        });
                    }
                });
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    _getQrCode: function(e) {
        var a = this;
        t._getNetWork({
            url: t.getNetAddresss("member.member.getArticleQr"),
            success: function(t) {
                if (1 == t.data.result) {
                    var i = t.data.data;
                    a.setData({
                        codeUrl: i
                    }), null != e && e(t);
                } else console.error(t);
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    _toLike: function(e) {
        var a = this;
        t._getNetWork({
            url: t.getNetAddresss("plugin.article.api.article.like"),
            data: {
                article_id: a.data.article_id
            },
            success: function(t) {
                1 == t.data.result ? (1 == t.data.data.liked ? (a.setData({
                    is_liked: !0,
                    like_num: a.data.like_num + 1
                }), wx.showToast({
                    title: "点赞成功"
                })) : (a.setData({
                    is_liked: !1,
                    like_num: a.data.like_num - 1
                }), a.data.like_num < 0 && a.setData({
                    like_num: 0
                }), wx.showToast({
                    title: "取消点赞",
                    icon: "none"
                })), e && e(t)) : console.error(t);
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    clickLike: function() {
        this._toLike();
    },
    toAdv: function() {
        console.log("跳转到广告"), !t._isTextEmpty(this.data.articleData) && t._isTextEmpty(this.data.articleData.min_app_advs_link) || wx.navigateTo({
            url: this.data.articleData.min_app_advs_link
        });
    },
    toLink: function(t) {
        var e = t.currentTarget.dataset.link;
        wx.navigateTo({
            url: e
        });
    },
    toReport: function() {
        wx.navigateTo({
            url: "/packageA/member/article/noticeReport/noticeReport?article_id=" + this.data.article_id
        });
    },
    previewImage: function(t) {
        var e = t.target.dataset.src;
        wx.previewImage({
            current: e,
            urls: [ e ]
        });
    },
    onReady: function() {},
    onShow: function() {
        wx.showShareMenu({
            withShareTicket: !1,
            menus: [ "shareAppMessage", "shareTimeline" ]
        }), "article" == this.data.options.task_type && this.shareArticleTask();
    },
    shareArticleTask: function() {
        var e = {
            record_id: this.data.options.taskID,
            task_type: "article",
            day: this.data.options.taskDay,
            task_record_id: this.data.article_id
        }, a = t.getNetAddresss("plugin.task-package.frontend.daily-record.finish-task");
        t._postNetWork({
            url: a,
            showToastIn: !1,
            data: e,
            success: function(t) {},
            fail: function(t) {}
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareTimeline: function() {
        var t = wx.getStorageSync("yz_uid"), e = "";
        t && (e = t);
        var a = {
            title: this.data.articleData.title,
            query: "mid=".concat(e, "&article_id=").concat(this.data.article_id),
            imageUrl: this.data.articleData.thumb ? this.data.articleData.thumb : ""
        };
        return this.data.options.taskID && (a.query += "&taskID=".concat(this.data.options.taskID, "&taskDay=").concat(this.data.options.taskDay, "&task_type=article")), 
        a;
    },
    onShareAppMessage: function() {
        var t = wx.getStorageSync("yz_uid"), e = "";
        t && (e = t);
        var a = {
            path: "/packageA/member/article/articleContent/articleContent?article_id=" + this.data.article_id + "&mid=" + e,
            title: this.data.articleData.title,
            imageUrl: this.data.articleData.thumb ? this.data.articleData.thumb : ""
        };
        return this.data.options.taskID && (a.path += "&taskID=".concat(this.data.options.taskID, "&taskDay=").concat(this.data.options.taskDay, "&task_type=article")), 
        a;
    },
    uploaderFun: function(e) {
        wx.showLoading({
            title: "加载中"
        });
        var a = this, i = t.getNetAddresss("upload.uploadPic");
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album", "camera" ],
            success: function(t) {
                var e = t.tempFilePaths[0];
                wx.uploadFile({
                    url: i,
                    filePath: e,
                    name: "file",
                    success: function(t) {
                        var e = JSON.parse(t.data);
                        if (wx.hideLoading(), 1 == e.result) {
                            var i = a.data.imgList;
                            i.push(e.data.img_url), a.setData({
                                imgList: i
                            });
                        } else wx.showToast({
                            icon: "none",
                            title: e.msg,
                            duration: 1500
                        });
                    }
                });
            },
            fail: function(t) {
                wx.hideLoading({
                    success: function(t) {}
                });
            }
        });
    },
    delCommentIMg: function(t) {
        var e = t.target.dataset.index;
        this.data.imgList.splice(e, 1), this.setData({
            imgList: this.data.imgList
        });
    },
    getComment: function() {
        var e = this;
        t._getNetWork({
            url: t.getNetAddresss("plugin.article.api.comment.comment-list"),
            data: {
                page: this.data.commentPage,
                article_id: this.data.article_id
            },
            success: function(t) {
                if (1 == t.data.result) {
                    for (var a = t.data.data.list.data, i = 0; i < a.length; i++) a[i].reply_num > 0 ? a[i].commentShow = !0 : a[i].commentShow = !1, 
                    a[i].commenMorePage = 1, a[i].child = [];
                    e.setData({
                        last_page: t.data.data.list.last_page,
                        commenMember: t.data.data.member,
                        commentList: t.data.data.list.data,
                        commentNum: t.data.data.num
                    });
                } else console.error(t);
            },
            fail: function(t) {
                console.error(t);
            }
        });
    },
    watchImg: function(t) {
        var e = t.currentTarget.dataset.item;
        wx.previewImage({
            current: e[0],
            urls: e
        });
    },
    getMore: function() {
        var e = this;
        this.setData({
            commentPage: this.data.commentPage + 1
        }), wx.showLoading({
            title: "加载中",
            duration: 1e3,
            icon: "none"
        }), t._getNetWork({
            url: t.getNetAddresss("plugin.article.api.comment.comment-list"),
            data: {
                page: this.data.commentPage,
                article_id: this.data.article_id
            },
            success: function(t) {
                if (1 == t.data.result) {
                    for (var a = t.data.data.list.data, i = 0; i < a.length; i++) a[i].reply_num > 0 ? a[i].commentShow = !0 : a[i].commentShow = !1, 
                    a[i].commenMorePage = 1, a[i].child = [];
                    e.setData({
                        commentList: e.data.commentList.concat(a),
                        last_page: t.data.data.list.last_page
                    });
                } else console.error(t);
                wx.hideLoading({
                    success: function(t) {}
                });
            },
            fail: function(t) {
                console.error(t), wx.hideLoading({
                    success: function(t) {}
                });
            }
        });
    },
    subMainCommentBtn: function() {
        this.setData({
            maincomment: "",
            mainCommentShow: !0
        });
    },
    mainCommentShowClose: function() {
        this.setData({
            mainCommentShow: !1
        });
    },
    replyshowClose: function() {
        this.setData({
            replyshow: !1
        });
    },
    maincommentinp: function(t) {
        var e = t.detail.value;
        this.setData({
            maincomment: e
        });
    },
    replyContentinp: function(t) {
        var e = t.detail.value;
        this.setData({
            replyContent: e
        });
    },
    replyBtn: function(t) {
        var e = t.currentTarget.dataset.item, a = e.nick_name;
        this.setData({
            keyInd: t.currentTarget.dataset.kind,
            replyshow: !0,
            replyId: e.id,
            comment_id: 0 == e.comment_id ? e.comment_id : e.id,
            replyName: a,
            commentManagement: !1
        });
    },
    subMainBtn: function(t) {
        var e = t.currentTarget.dataset.type;
        this.getReplyComments(e);
    },
    getReplyComments: function(e) {
        var a, i = this, n = {
            images: i.data.imgList,
            content: ""
        };
        if (console.log(e), "main" == e) {
            if (t._isTextEmpty(this.data.maincomment)) return void wx.showToast({
                title: "请输入评论内容",
                icon: "none",
                duration: 1e3
            });
            a = "plugin.article.api.comment.create-comment", n.article_id = i.data.article_id, 
            n.content = i.data.maincomment;
        } else if ("reply" == e) {
            if (t._isTextEmpty(this.data.replyContent)) return void wx.showToast({
                title: "请输入评论内容",
                icon: "none",
                duration: 1e3
            });
            a = "plugin.article.api.comment.reply-comment", n.content = i.data.replyContent, 
            n.comment_id = i.data.replyId;
        }
        wx.showLoading({
            title: "加载中",
            duration: 1e3,
            icon: "none"
        }), t._getNetWork({
            url: t.getNetAddresss(a),
            data: n,
            success: function(t) {
                1 == t.data.result ? "main" == e ? (i.setData({
                    commentPage: 1,
                    mainCommentShow: !1,
                    replyshow: !1,
                    maincomment: ""
                }), i.getComment()) : "reply" == e && i.updateInfo(t.data.data, i.data.keyInd) : console.error(t), 
                wx.hideLoading({
                    success: function(t) {}
                });
            },
            fail: function(t) {
                wx.hideLoading({
                    success: function(t) {}
                }), console.error(t);
            }
        });
    },
    updateInfo: function(t, e) {
        console.log(t, e);
        var a = [];
        a.push(t), this.data.commentList[e].child ? this.data.commentList[e].child.unshift(a[0]) : this.data.commentList[e].child = a, 
        this.setData({
            commentList: this.data.commentList,
            mainCommentShow: !1,
            replyshow: !1,
            replyContent: ""
        });
    },
    moreCommentLis: function(e) {
        console.log(e), wx.showLoading({
            title: "加载中",
            icon: "none",
            duration: 1e3
        });
        var a = this, i = e.currentTarget.dataset.item.id, n = e.currentTarget.dataset.ind;
        console.log(this.data.commentList);
        var o = Number(this.data.commentList[n].commenMorePage), s = {
            page: o,
            comment_id: i
        };
        t._getNetWork({
            url: t.getNetAddresss("plugin.article.api.comment.get-replys"),
            data: s,
            success: function(t) {
                if (1 == t.data.result) {
                    var e = t.data.data;
                    console.log(e);
                    var i = e.data, s = a.data.commentList[n].reply_num - e.data.length;
                    a.data.commentList[n].reply_num = s;
                    var c = a.data.commentList[n].child.concat(i);
                    a.data.commentList[n].child = c;
                    var r = e.data.last_page;
                    o >= r ? o = 1 : o += 1, a.data.commentList[n].commenMorePage = o, a.setData({
                        commentList: a.data.commentList
                    });
                } else console.error(t);
                wx.hideLoading({
                    success: function(t) {}
                });
            },
            fail: function(t) {
                wx.hideLoading({
                    success: function(t) {}
                }), console.error(t);
            }
        });
    },
    delComment: function(t) {
        var e;
        console.log(t);
        try {
            e = wx.getStorageSync("uid");
        } catch (t) {
            e = "";
        }
        var a, i = t.currentTarget.dataset.item, n = t.currentTarget.dataset.ind;
        try {
            a = t.currentTarget.dataset.index;
        } catch (t) {
            a = null;
        }
        console.log(i, n, a), this.data.commentList[n].child.length > 0 ? i.uid == e && (console.log("子评论是我的，可以删"), 
        this.delRequst(i.id, n, a, "child")) : e == i.uid && (console.log("主评论是我的，可以删"), 
        this.delRequst(i.id, n, a, "list"));
    },
    delRequst: function(e, a, i, n) {
        var o = this;
        wx.showModal({
            content: "是否删除评论",
            success: function(s) {
                s.confirm && (console.log("删除"), wx.showLoading({
                    title: "加载中",
                    duration: 1e3,
                    icon: "none"
                }), t._getNetWork({
                    url: t.getNetAddresss("plugin.article.api.comment.del"),
                    data: {
                        comment_id: e
                    },
                    success: function(t) {
                        1 == t.data.result ? ("child" == n ? o.data.commentList[a].child.splice(i, 1) : o.data.commentList.splice(a, 1), 
                        o.setData({
                            commentList: o.data.commentList
                        }), wx.showToast({
                            title: t.data.data,
                            icon: "none",
                            duration: 1e3
                        })) : console.error(t), wx.hideLoading({
                            success: function(t) {}
                        });
                    },
                    fail: function(t) {
                        console.error(t), wx.hideLoading({
                            success: function(t) {}
                        });
                    }
                }));
            }
        });
    }
});